<?php  
class ControllerProductSanPham extends Controller {
	public function index() {
		$this->language->load('common/home');
		$this->load->model('tool/seo_url');
		$this->load->helper('image');
		
		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
      		'href'      => $this->model_tool_seo_url->rewrite($this->url->http('common/home')),
       		'text'      => $this->language->get('text_home'),
       		'separator' => FALSE
   		);
		$this->document->breadcrumbs[] = array(
			'href'      => $this->model_tool_seo_url->rewrite($this->url->http('product/sanpham')),
			'text'      => 'Sản phẩm',
			'separator' => $this->language->get('text_separator')
		);
		$this->document->title = 'Sản phẩm';
		
		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_store'));

// danh muc
		$this->load->model('catalog/category');
		$this->data['categories'] = $this->getCategories(0);
// end danh muc


				
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/sanpham.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/product/sanpham.tpl';
		} else {
			$this->template = 'default/template/product/sanpham.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	private function getCategories($parent_id, $current_path = '', $level = 0) {
		$level++;
		$data = array();
		
		$results = $this->model_catalog_category->getCategories($parent_id);
		
		foreach ($results as $result) {
			if (!$current_path) {
				$new_path = $result['category_id'];
			} else {
				$new_path = $current_path . '_' . $result['category_id'];
			}
			if ($result['image']) {
				$image = $result['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$children = $this->getCategories($result['category_id'], $new_path, $level);
			$data[] = array(
				'category_id' => $result['category_id'],
				'href'        => $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $new_path)),
				'children'		  => $children,
				'thumb'   => image_resize($image, 150,150),
				'name'        => $result['name']
			);
		}
		
		return $data;
	}
}
?>